package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.dao.CommonDAO;
import com.model.Goods;
import com.model.Jieyuerecord;
import com.model.Kcrecord;
import com.model.Member;
import com.model.News;

import com.model.Sysuser;
import com.model.Yqlink;
import com.opensymphony.xwork2.ActionSupport;
import com.util.Info;
import com.util.MD5;
import com.util.Pagination;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

/**
 * 购物车功能
 * 
 * @author Administrator
 * 
 */
public class JieyuerecordAction extends ActionSupport {
	private Integer id;
	private String memberid;
	private String gid;
	private String stime;
	private String etime;
	private int fakuan;
	String suc;
	String no;
	private int index = 1;
	private CommonDAO commonDAO;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getMemberid() {
		return memberid;
	}

	public void setMemberid(String memberid) {
		this.memberid = memberid;
	}

	public String getGid() {
		return gid;
	}

	public void setGid(String gid) {
		this.gid = gid;
	}

	public String getStime() {
		return stime;
	}

	public void setStime(String stime) {
		this.stime = stime;
	}

	public String getEtime() {
		return etime;
	}

	public void setEtime(String etime) {
		this.etime = etime;
	}

	public String getSuc() {
		return suc;
	}

	public void setSuc(String suc) {
		this.suc = suc;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public CommonDAO getCommonDAO() {
		return commonDAO;
	}

	public void setCommonDAO(CommonDAO commonDAO) {
		this.commonDAO = commonDAO;
	}
	
	

	
	
	public int getFakuan() {
		return fakuan;
	}

	public void setFakuan(int fakuan) {
		this.fakuan = fakuan;
	}

	//借出
	public String jiechuAdd() {
		Jieyuerecord j = (Jieyuerecord)commonDAO.findById(id, "Jieyuerecord");
		Kcrecord kcrecord = (Kcrecord)commonDAO.findByHql("from Kcrecord where gid='"+j.getGid()+"'").get(0);
		if(Integer.parseInt(kcrecord.getHappennum())>0){
		
		Goods g = (Goods)commonDAO.findById(j.getGid(), "Goods");
		Member m  = (Member)commonDAO.findById(j.getMemberid(), "Member");
		ArrayList<Jieyuerecord> list = (ArrayList<Jieyuerecord>)commonDAO.findByHql("from Jieyuerecord where memberid='"+memberid+"' and iszj='在借'");
		int updatenum = Integer.parseInt(kcrecord.getHappennum())-1;
		kcrecord.setHappennum(String.valueOf(updatenum));
		j.setIszj("在借");
		j.setIsyq("");
		j.setYqday(0);
		j.setDelstatus("0");
		j.setSaver(g.getSaver());
		g.setIszy("在借");
		commonDAO.update(g);
		commonDAO.update(kcrecord);
		commonDAO.update(j);
		suc = "操作成功";
		return "success";
		}else{
		suc="库存不足";
		return "error";	
		}
	}
	
	//还书
	public String huanShu() {
		Jieyuerecord j = (Jieyuerecord)commonDAO.findById(id, "Jieyuerecord");
		Member m = (Member)commonDAO.findById(j.getMemberid(), "Member");
		Goods g = (Goods)commonDAO.findById(j.getGid(), "Goods");
		Kcrecord kcrecord = (Kcrecord)commonDAO.findByHql("from Kcrecord where gid='"+j.getGid()+"'").get(0);
		int updatenum = Integer.parseInt(kcrecord.getHappennum())+1;
		kcrecord.setHappennum(String.valueOf(updatenum));
		String dx = Info.compare_datezq(j.getEtime(), Info.getDateStr().substring(0,10));
		int yqday =0;
		if(dx.equals("small")){
			yqday = Info.dayToday(j.getEtime()+" 00:00:00", Info.getDateStr());
		}
		j.setIszj("已归还");
		if(yqday>0){
		j.setIsyq("已逾期");
		j.setYqday(yqday);
		}else if(yqday==0){
			j.setIsyq("正常");
			j.setYqday(yqday);
		}
		g.setIszy("在库");
		g.setCs(g.getCs()+1);
		commonDAO.update(j);
		commonDAO.update(g);
		commonDAO.update(kcrecord);
		return "success";
		
	}
	
	public String yuyueBook() {
		Member m  = (Member)commonDAO.findById(memberid, "Member");
		Goods goods  = (Goods)commonDAO.findById(gid, "Goods");
		Jieyuerecord jy = new Jieyuerecord();
		jy.setStime(Info.getDateStr());
		jy.setEtime(etime);
		jy.setMemberid(memberid);
		jy.setGid(gid);
		jy.setIszj("");
		jy.setIsyq("");
		jy.setYqday(0);
		jy.setFakuan(0);
		jy.setDelstatus("0");
		jy.setSaver(goods.getSaver());
		commonDAO.save(jy);
		gid=gid;
		suc = "操作成功";
		return "success";
	}
	
	//删除预约
	public String yueyeDel() {
		Jieyuerecord j  = (Jieyuerecord)commonDAO.findById(id, "Jieyuerecord");
		commonDAO.delete(j);
		suc = "取消成功";
		return "success";
	}
	
	//删除操作完成的借阅
	public String jieyuerecordDel() {
		Jieyuerecord j  = (Jieyuerecord)commonDAO.findById(id, "Jieyuerecord");
		j.setDelstatus("1");
		commonDAO.update(j);
		suc = "删除成功";
		return "success";
	}
	
	//删除操作完成的借阅
	public String yjjieyuerecordDel() {
		Jieyuerecord j  = (Jieyuerecord)commonDAO.findById(id, "Jieyuerecord");
		commonDAO.delete(j);
		suc = "取消成功";
		return "success";
	}
	
	public String fakuanedit() {
		Jieyuerecord j  = (Jieyuerecord)commonDAO.findById(id, "Jieyuerecord");
		j.setFakuan(j.getFakuan()+fakuan);
		commonDAO.update(j);
		suc = "操作成功";
		return "success";
	}

}
